﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CVerticalSelectorData))]
    public class CVerticalSelector : CSelector
    {
        #region actualize

        /// <summary>
        /// Actualizes the widget.
        /// If anything has changed, it will allow this widget to get back its parameters.
        /// For example, if the size of the widget is a percentage of the size of its parent,
        /// if the parent's size changes, it will recalculate the good size of this widget.
        /// (This method is called automatically but you can force a new actualization).
        /// </summary>
        public override void Actualize()
        {
            m_bodyRect.Y = m_rect.Y + m_nextButton.Rect.Height;
            m_bodyRect.Height = m_rect.Height - m_prevButton.Rect.Height - m_nextButton.Rect.Height;
            
            m_nextButton.Position = new Vector2((m_rect.Width / 2) - (m_nextButton.Rect.Width / 2), 0);
            m_nextButton.Actualize();

            m_prevButton.Position = new Vector2((m_rect.Width / 2) - (m_prevButton.Rect.Width / 2), m_rect.Height - m_prevButton.Rect.Height);
            m_prevButton.Actualize();
        }

        #endregion

        #region actionsOnm_list

        /// <summary>
        /// Adds a widget in the horizontal selector
        /// </summary>
        public CVerticalSelector Add(CWidget widget)
        {
            widget.Size = new Vector2(1, 1);
            widget.Position = new Vector2(0, 0);
            widget.Rect = m_bodyRect;
            m_list.Add(widget);
            return this;
        }

        #endregion

        #region ctor

        public CVerticalSelector(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_nextButton.Position = new Vector2((m_rect.Width / 2) - (m_nextButton.Rect.Width / 2), 0);
            m_nextButton.Texture = HUD.Skinner.GetTexture("verticalselectorlessbuttonreleased");
            m_nextButton.ReleasedTexture = HUD.Skinner.GetTexture("verticalselectorlessbuttonreleased");
            m_nextButton.ClickedTexture = HUD.Skinner.GetTexture("verticalselectorlessbuttonclicked");
            
            m_prevButton.Position = new Vector2((m_rect.Width / 2) - (m_prevButton.Rect.Width / 2), m_rect.Height - m_prevButton.Rect.Height);
            m_prevButton.Texture = HUD.Skinner.GetTexture("verticalselectormorebuttonreleased");
            m_prevButton.ReleasedTexture = HUD.Skinner.GetTexture("verticalselectormorebuttonreleased");
            m_prevButton.ClickedTexture = HUD.Skinner.GetTexture("verticalselectormorebuttonclicked");

            m_texture = HUD.Skinner.GetTexture("verticalselectorbackground");
            m_bodyRect.Y = m_rect.Y + m_nextButton.Rect.Height;
            m_bodyRect.Height = m_rect.Height - m_prevButton.Rect.Height - m_nextButton.Rect.Height;

            Size = new Vector2(400, 400);
        }

        #endregion

        #region update/draw

        public override bool Update(GameTime gameTime)
        {
            if (m_list.Count > 0)
                m_selected = m_list[m_selectedIndex % m_list.Count];
            return base.Update(gameTime);
        }

        public override bool Draw(GameTime gameTime)
        {
            if (!m_isVisible)
                return false;



            if (m_texture != null)
                m_texture.Draw(gameTime);//m_spriteBatch.Draw(m_texture, m_rect, m_color);
            if (m_selected != null)
            {
                m_selected.Rect = m_bodyRect;
                m_selected.Draw(gameTime);
            }

            m_nextButton.Draw(gameTime);
            m_prevButton.Draw(gameTime);



            return true;
        }

        #endregion

    }
}
